﻿using System.Collections;
using System.Windows;
using System.Windows.Controls;

namespace Hopscotch.Presentation.Controls
{
	public class DynamicPanel : ContentControl
	{
		Grid grid;

		public DynamicPanel()
		{
			grid = new Grid();

			

			//zxzx
			//for(int i=0; i<3; i++)
			//{
			//	grid.RowDefinitions.Add(new RowDefinition());
			//	grid.Children.Add(new TextBox(){Text="Hello"});
			//}

			this.Content = grid;
		}

		public ArrayList AvailableElements
		{
			get { return (ArrayList)this.GetValue(AvailableElementsProperty); }
			set { this.SetValue(AvailableElementsProperty, value); } 
		}
		
		public static readonly DependencyProperty AvailableElementsProperty
			  = DependencyProperty.Register("AvailableElements", typeof(ArrayList), typeof(DynamicPanel), new PropertyMetadata(null));

		//public static readonly DependencyProperty EditItemsProperty
		//	  = DependencyProperty.Register("EditItems", typeof(bool), typeof(DynamicPanel),
		//	  new PropertyMetadata(false, new PropertyChangedCallback(OnEditItemsChanged) ));

		//public bool EditItems
		//{
		//	get { return (bool)GetValue(EditItemsProperty); }
		//	set { SetValue(EditItemsProperty, value); }
		//}

		//private static void OnEditItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		//{
		//	var dynamicPanel = d as DynamicPanel;
		//	if (dynamicPanel != null)
		//	{
		//		dynamicPanel.Popup();
		//	}
		//}

		private void ShowItemsDialog()
		{
			ItemsWindow w = new ItemsWindow();

			Window parent = Window.GetWindow(this);
			w.Owner = parent;
			w.Show();
		}


		public void StartEdit()
		{
			ShowItemsDialog();

			grid.ShowGridLines = true;
			int row=0;

			foreach (object obj in AvailableElements)
			{
				UIElement uiElement = obj as UIElement;
				if (uiElement != null)
				{
					grid.RowDefinitions.Add(new RowDefinition());
					Grid.SetRow(uiElement, row);
					row++;

					grid.Children.Add(uiElement);
				}

			}

		}
	}
}
